package com.sunflower.common.mybatis.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;

import java.util.Collection;

/**
 * @author: SunLuQuan
 * @Date: 2024年03月26日 17:26
 * @Description: QueryWrapperUtil
 * TODO
 */
public class QueryWrapperUtil {


    public static <T, V> LambdaQueryWrapper<T> eqOrIn(SFunction<T, V> fieldSFun, Collection<V> values, SFunction<T, ?>... columns) {
        LambdaQueryWrapper<T> queryWrapper = Wrappers.lambdaQuery();
        eqOrIn(queryWrapper,fieldSFun,values,columns);
        return queryWrapper;
    }

    public static <T, V> void eqOrIn(LambdaQueryWrapper<T> queryWrapper, SFunction<T, V> fieldSFun, Collection<V> values, SFunction<T, ?>... columns) {
        queryWrapper.select(columns);
        if (values.size() > 1) {
            queryWrapper.in(fieldSFun, values);
        } else {
            queryWrapper.eq(fieldSFun, CollectionUtil.getFirst(values));
        }
    }
}
